package com.ca.project.web.Serializer;

import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.Date;
import java.util.Objects;

/**
 * 后台返回的data数据给他重新序列化
 * Date转String的JSON序列化器
 * 用于返回实体Date类型字段的自动序列化
 *
 * @PACKAGE_NAME: com.ca.project.web.Serializer
 * @NAME: Date2StringsSerializer
 * @USER: ASUS
 * @DATE: 2023/8/28
 * @TIME: 上午 10:02
 **/
public class Date2StringsSerializer extends JsonSerializer<Date> {
    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (Objects.isNull(date)) {
            jsonGenerator.writeString(StringUtils.EMPTY);
        } else {
            jsonGenerator.writeString(DateUtil.formatDateTime(date));
        }
    }

}